﻿using AwesomiumDotNet;
using System;
using System.IO;
using System.Threading;

namespace BasicSample
{
    class BasicSample
    {
        int Width = 800;
        int Height = 600;
        string URL = "http://www.google.com";

        bool isRunning = true;
        WebView webView;

        public BasicSample()
        {
            WebCoreConfig config = new WebCoreConfig();
            //config.SetEnablePlugins(true);
            config.SetLogLevel(LogLevel.None);
            WebCore webCore = new WebCore(config);

            webView = webCore.CreateWebView(Width, Height);
            webView.BeginNavigation += OnBeginNavigation;
            webView.FinishLoading += OnFinishLoading;

            webView.LoadUrl(URL);
            //webView.LoadFile("index.html");

            while (isRunning)
            {
                Thread.Sleep(250);
                webCore.Update();
            }

            System.Console.WriteLine("Done.\n");

            webView.Dispose();
            webCore.Dispose();
        }

        private void OnBeginNavigation(object sender, BeginNavigationEventArgs e)
        {
            System.Console.WriteLine("Navigating to " + e.Url);
        }

        private void OnFinishLoading(object sender, FinishLoadingEventArgs e)
        {
            RenderBuffer renderBuffer = webView.Render();
            renderBuffer.SaveToPng("result.png", false);

            isRunning = false;
        }
    }
}
